{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Numpy的数学统计函数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "本节内容：\n",
    "\n",
    "#### 1、Numpy有哪些数学统计函数：  \n",
    "<table style=\"margin-left:0px; text-align:left;\">\n",
    "    <tr><th>函数名</th><th>说明</th></tr>\n",
    "    <tr><td>np.sum</td><td>所有元素的和</td></tr>\n",
    "    <tr><td>np.prod</td><td>所有元素的乘积</td></tr>\n",
    "    <tr><td>np.cumsum</td><td>元素的累积加和</td></tr>\n",
    "    <tr><td>np.cumprod</td><td>元素的累积乘积</td></tr>\n",
    "    <tr><td>np.min</td><td>最小值</td></tr>\n",
    "    <tr><td>np.max</td><td>最大值</td></tr>\n",
    "    <tr><td>np.percentile</td><td>0-100百分位数</td></tr>\n",
    "    <tr><td>np.quantile</td><td>0-1分位数</td></tr>\n",
    "    <tr><td>np.median</td><td>中位数</td></tr>\n",
    "    <tr><td>np.average</td><td>加权平均，参数可以指定weights</td></tr>\n",
    "    <tr><td>np.mean</td><td>平均值</td></tr>\n",
    "    <tr><td>np.std</td><td>标准差</td></tr>\n",
    "    <tr><td>np.var</td><td>方差</td></tr>\n",
    "</table>\n",
    "\n",
    "#### 2、怎样实现按不同的axis计算\n",
    "以上函数，都有一个参数叫做axis用于指定计算轴为行还是列，如果不指定，那么会计算所有元素的结果\n",
    "\n",
    "#### 3、实例：机器学习将数据进行标准化\n",
    "A = (A - mean(A, axis=0)) / std(A, axis=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1、Numpy的数学统计函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([[ 0,  1,  2,  3],\n       [ 4,  5,  6,  7],\n       [ 8,  9, 10, 11]])"
     },
     "metadata": {},
     "output_type": "execute_result",
     "execution_count": 105
    }
   ],
   "source": [
    "arr = np.arange(12).reshape(3,4)\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "66"
     },
     "metadata": {},
     "output_type": "execute_result",
     "execution_count": 106
    }
   ],
   "source": [
    "np.sum(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "0"
     },
     "metadata": {},
     "output_type": "execute_result",
     "execution_count": 107
    }
   ],
   "source": [
    "np.prod(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([ 0,  1,  3,  6, 10, 15, 21, 28, 36, 45, 55, 66], dtype=int32)"
     },
     "metadata": {},
     "output_type": "execute_result",
     "execution_count": 108
    }
   ],
   "source": [
    "np.cumsum(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=int32)"
     },
     "metadata": {},
     "output_type": "execute_result",
     "execution_count": 109
    }
   ],
   "source": [
    "np.cumprod(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "0"
     },
     "metadata": {},
     "output_type": "execute_result",
     "execution_count": 110
    }
   ],
   "source": [
    "np.min(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "11"
     },
     "metadata": {},
     "output_type": "execute_result",
     "execution_count": 111
    }
   ],
   "source": [
    "np.max(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([2.75, 5.5 , 8.25])"
     },
     "metadata": {},
     "output_type": "execute_result",
     "execution_count": 112
    }
   ],
   "source": [
    "# 50 表示整体序列的中间那个数据\n",
    "np.percentile(arr, [25, 50, 75])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([2.75, 5.5 , 8.25])"
     },
     "metadata": {},
     "output_type": "execute_result",
     "execution_count": 113
    }
   ],
   "source": [
    "np.quantile(arr, [0.25, 0.5, 0.75])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "5.5"
     },
     "metadata": {},
     "output_type": "execute_result",
     "execution_count": 114
    }
   ],
   "source": [
    "# 中位数\n",
    "np.median(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "5.5"
     },
     "metadata": {},
     "output_type": "execute_result",
     "execution_count": 115
    }
   ],
   "source": [
    "np.mean(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "3.452052529534663"
     },
     "metadata": {},
     "output_type": "execute_result",
     "execution_count": 116
    }
   ],
   "source": [
    "# 标准差\n",
    "np.std(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "11.916666666666666"
     },
     "metadata": {},
     "output_type": "execute_result",
     "execution_count": 117
    }
   ],
   "source": [
    "# 方差\n",
    "np.var(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "4.804766742874184"
     },
     "metadata": {},
     "output_type": "execute_result",
     "execution_count": 118
    }
   ],
   "source": [
    "# weights的shape需要和arr一样，加权求和\n",
    "# * 星号表示收集任意数量的实参\n",
    "weights = np.random.rand(*arr.shape)\n",
    "np.average(arr, weights=weights)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2、Numpy的axis参数的用途"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "axis=0代表行、axis=1代表列\n",
    "\n",
    "对于sum/mean/media等聚合函数：\n",
    "* 理解1：axis=0代表把行消解掉，axis=1代表把列消解掉\n",
    "* 理解2：axis=0代表跨行计算，axis=1代表跨列计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([[ 0,  1,  2,  3],\n       [ 4,  5,  6,  7],\n       [ 8,  9, 10, 11]])"
     },
     "metadata": {},
     "output_type": "execute_result",
     "execution_count": 119
    }
   ],
   "source": [
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([12, 15, 18, 21])"
     },
     "metadata": {},
     "output_type": "execute_result",
     "execution_count": 120
    }
   ],
   "source": [
    "arr.sum(axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([ 6, 22, 38])"
     },
     "metadata": {},
     "output_type": "execute_result",
     "execution_count": 121
    }
   ],
   "source": [
    "arr.sum(axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([[ 0,  1,  2,  3],\n       [ 4,  6,  8, 10],\n       [12, 15, 18, 21]], dtype=int32)"
     },
     "metadata": {},
     "output_type": "execute_result",
     "execution_count": 122
    }
   ],
   "source": [
    "arr.cumsum(axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([[ 0,  1,  3,  6],\n       [ 4,  9, 15, 22],\n       [ 8, 17, 27, 38]], dtype=int32)"
     },
     "metadata": {},
     "output_type": "execute_result",
     "execution_count": 123
    }
   ],
   "source": [
    "arr.cumsum(axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3、实例：机器学习将数据进行标准化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([[ 0,  1,  2,  3],\n       [ 4,  5,  6,  7],\n       [ 8,  9, 10, 11]])"
     },
     "metadata": {},
     "output_type": "execute_result",
     "execution_count": 124
    }
   ],
   "source": [
    "arr"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "arr如果对应到现实世界的一种解释：\n",
    "* 行：每行对应一个样本数据\n",
    "* 列：每列代表样本的一个特征\n",
    "\n",
    "数据标准化：  \n",
    "* 对于机器学习、神经网络来说，不同列的量纲应该相同，训练收敛的更快；\n",
    "* 比如商品的价格是0到100元、销量是1万到10万个，这俩数字没有可比性，因此需要先都做标准化；\n",
    "* 不同列代表不同的特征，因此需要axis=0做计算\n",
    "* 标准化一般使用A = (A - mean(A, axis=0)) / std(A, axis=0)公式进行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([4., 5., 6., 7.])"
     },
     "metadata": {},
     "output_type": "execute_result",
     "execution_count": 125
    }
   ],
   "source": [
    "# 计算每列的均值\n",
    "mean = np.mean(arr, axis=0)\n",
    "mean"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([3.26598632, 3.26598632, 3.26598632, 3.26598632])"
     },
     "metadata": {},
     "output_type": "execute_result",
     "execution_count": 126
    }
   ],
   "source": [
    "# 计算每列的方差\n",
    "std = np.std(arr, axis=0)\n",
    "std"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([[-4., -4., -4., -4.],\n       [ 0.,  0.,  0.,  0.],\n       [ 4.,  4.,  4.,  4.]])"
     },
     "metadata": {},
     "output_type": "execute_result",
     "execution_count": 127
    }
   ],
   "source": [
    "# 计算分子，注意每行都会分别减去[4., 5., 6., 7.]，这叫做numpy的广播\n",
    "fenzi = arr-mean\n",
    "fenzi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([[-1.22474487, -1.22474487, -1.22474487, -1.22474487],\n       [ 0.        ,  0.        ,  0.        ,  0.        ],\n       [ 1.22474487,  1.22474487,  1.22474487,  1.22474487]])"
     },
     "metadata": {},
     "output_type": "execute_result",
     "execution_count": 128
    }
   ],
   "source": [
    "result = fenzi/std\n",
    "result"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 用随机数再试一次"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([[13, 40, 41, 56],\n       [24,  8,  4, 26],\n       [21, 56, 69, 53]])"
     },
     "metadata": {},
     "output_type": "execute_result",
     "execution_count": 129
    }
   ],
   "source": [
    "arr2 = np.random.randint(1, 100, size=(3,4))\n",
    "arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([[-1.3641205 ,  0.26726124,  0.11269386,  0.81537425],\n       [ 1.00514142, -1.33630621, -1.27719707, -1.4083737 ],\n       [ 0.35897908,  1.06904497,  1.16450321,  0.59299945]])"
     },
     "metadata": {},
     "output_type": "execute_result",
     "execution_count": 130
    }
   ],
   "source": [
    "result = (arr2 - np.mean(arr2,axis=0)) / np.std(arr2,axis=0)\n",
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "source": [],
    "metadata": {
     "collapsed": false
    }
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}